﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.IO;
using IWshRuntimeLibrary;// windows Script Host
/*
Copyright 2011 Mirosław M. Mączka

This file is part of MyChars.

    Flac2Wma is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
namespace CreateShortcutOnDesktop
{
    public sealed class CreateShortcutOnDesktop
    {
        /// <summary>
        /// Tworzy ikonę na desktopie - tylko wtedy, gdy program nie został zainstalowany przez ClickOnce
        /// </summary>
        public static void Do()
        {
#if true
            //
            string uninstallReg = @"Software\Microsoft\Windows\CurrentVersion\Uninstall";
            string apprefPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "MyChars.appref-ms");
            string iconPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "MyChars.lnk");
            //
            try
            {
                //
                if (System.IO.File.Exists(apprefPath)) return;//istnieje ikona z ClickOnce
                if (System.IO.File.Exists(iconPath)) return;
                //
                string[] ss = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(uninstallReg).GetSubKeyNames();
                foreach (string a in ss)
                {
                    object o = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(uninstallReg + @"\" + a).GetValue("DisplayName");
                    string appName = o as string;
                    if (null == appName) continue;
                    if ("MyChars" == appName) return;//zainstalowany przez ClickOnce, ikona była utworzona
                }
                //
                //
                if (System.IO.File.Exists("NoIcon.txt")) return;
                //
                string exe = System.Reflection.Assembly.GetExecutingAssembly().Location;
                WshShell shell = new WshShell();
                //
                IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(iconPath);
                shortcut.Description = System.IO.Path.GetFileNameWithoutExtension(exe);
                //shortcut.Hotkey = "Ctrl+Shift+C";
                shortcut.TargetPath = exe;
                shortcut.Save();
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message, "ERROR(Icon)");
            }
#endif
        }
        //
    }//class
}//class
